1
'****************************** Module Header ******************************\
2 ' Module Name: Program.cs
4 ' Copyright (c) Microsoft Corporation.
6 ' This sample project shows how to use XPathDocument class to load the XML file
7 ' and manipulate. It includes two main parts, XPathNavigator usage and XPath
8 ' Expression usage. The first part shows how to use XPathNavigator to navigate
9 ' through the whole document, read its content. The second part shows how to
10 ' used XPath expression to filter information and select it out.
12 ' This source is subject to the Microsoft Public License.
13 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 ' All other rights reserved.
16 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
17 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
19 '***************************************************************************/
21 #Region
"Imports directives"
23 Imports System
.Xml
.XPath
31 'Initialize XPathDocument and XPathNavigator
32 Dim nav
As XPathNavigator
33 Dim xPathNavigator
As XPathNavigator
= New XPathDocument("books.xml").CreateNavigator
35 'Navigate through the document
36 xPathNavigator
.MoveToRoot()
37 xPathNavigator
.MoveToFirstChild()
38 If ((xPathNavigator
.NodeType
= XPathNodeType
.Element
) AndAlso xPathNavigator
.HasChildren
) Then
39 xPathNavigator
.MoveToFirstChild()
41 If xPathNavigator
.HasAttributes
Then
42 Console
.WriteLine(("Book ID: " & xPathNavigator
.GetAttribute("id", "")))
44 If xPathNavigator
.HasChildren
Then
45 xPathNavigator
.MoveToFirstChild()
47 Console
.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), xPathNavigator
.Name
, xPathNavigator
.Value
)
48 Loop While xPathNavigator
.MoveToNext
49 xPathNavigator
.MoveToParent()
51 Loop While xPathNavigator
.MoveToNext
54 'Use XPath Expression to select out the book with ID bk103
55 Console
.WriteLine("Use XPath Expression to select out the book with ID bk103:")
56 Dim expression
As XPathExpression
= xPathNavigator
.Compile("/catalog/book[@id='bk103']")
57 Dim iterator
As XPathNodeIterator
= xPathNavigator
.Select(expression
)
58 Do While iterator
.MoveNext
59 nav
= iterator
.Current
.Clone
60 Console
.WriteLine(("Book ID: " & nav
.GetAttribute("id", "")))
61 If nav
.HasChildren
Then
62 nav
.MoveToFirstChild()
64 Console
.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav
.Name
, nav
.Value
)
65 Loop While nav
.MoveToNext
69 'Use XPath Expression to select out all books whose price are more than 10
70 Console
.WriteLine(ChrW(13) & ChrW(10) & "Use XPath Expression to select out all books whose price are more than 10:")
71 expression
= xPathNavigator
.Compile("/catalog/book[price>10]")
72 iterator
= xPathNavigator
.Select(expression
)
73 Do While iterator
.MoveNext
74 nav
= iterator
.Current
.Clone
75 Console
.WriteLine(("Book ID: " & nav
.GetAttribute("id", "")))
76 If nav
.HasChildren
Then
77 nav
.MoveToFirstChild()
79 If (nav
.Name
= "title") Then
80 Console
.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav
.Name
, nav
.Value
)
82 If (nav
.Name
= "price") Then
83 Console
.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav
.Name
, nav
.Value
)
85 Loop While nav
.MoveToNext
89 'Use XPath Expression to calculate the average price of all books.
90 Console
.WriteLine(ChrW(13) & ChrW(10) & "Use XPath Expression to calculate the average price of all books:")
91 expression
= xPathNavigator
.Compile("sum(/catalog/book/price) div count(/catalog/book/price)")
92 Dim averagePrice
As String = xPathNavigator
.Evaluate(expression
).ToString
93 Console
.WriteLine("The average price of the books are {0}", averagePrice
)
95 'End. Read a char to exit
96 Console
.WriteLine("Input any key to quit the sample application")